package main

import (
	"bytes"
	"encoding/json"
	"net/http"
)

func forwardToWiseModel(namespace, modelName, apiKey string, req *ChatCompletionRequest) (*ChatCompletionResponse, error) {
	// 构建 WiseModel API 请求
	wiseReq := map[string]interface{}{
		"model":       "Qwen2",
		"messages":    req.Messages,
		"temperature": req.Temperature,
		"top_p":       req.TopP,
		"max_tokens":  req.MaxTokens,
		"stream":      req.Stream,
	}

	jsonData, err := json.Marshal(wiseReq)
	if err != nil {
		return nil, err
	}

	// 创建请求
	url := "https://wisemodel.cn/apiserving/"
	request, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		return nil, err
	}

	// 设置请求头
	request.Header.Set("Content-Type", "application/json")
	request.Header.Set("publisher-name", namespace)
	request.Header.Set("api-key", apiKey)
	request.Header.Set("serving-name-en", modelName)

	// 发送请求
	client := &http.Client{}
	resp, err := client.Do(request)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	// 解析响应
	var wiseResp ChatCompletionResponse
	if err := json.NewDecoder(resp.Body).Decode(&wiseResp); err != nil {
		return nil, err
	}

	return &wiseResp, nil
}
